package com.sunyur.tailor.channel.web.outer.inventory.controller;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.base.ValidList;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.BeanCopyUtil;
import com.sunyur.tailor.channel.service.inner.inventory.param.InventoryParam;
import com.sunyur.tailor.channel.service.inner.inventory.param.InventoryReleaseOrReduceParam;
import com.sunyur.tailor.channel.service.inner.inventory.param.InventorySelectParam;
import com.sunyur.tailor.channel.service.inner.inventory.service.SunyurInventoryService;
import com.sunyur.tailor.channel.web.outer.inventory.dto.InventoryDTO;
import com.sunyur.tailor.channel.web.outer.inventory.dto.InventoryReleaseOrReduceDTO;
import com.sunyur.tailor.channel.web.outer.inventory.dto.InventorySelectDTO;
import com.sunyur.tailor.channel.web.outer.inventory.dto.RecordParamList;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author xuhui
 * @Description 库存台账控制层
 * @Date 15:45 2021/6/1
 **/
@Slf4j
@RestController
@RequestMapping("/outer/inventory")
public class InventoryController {

    @Autowired
    private SunyurInventoryService sunyurInventoryService;

    /**
     * 批量创建库存台账
     */
    @PostMapping(value = "/batchInsert", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult batchInsert(@RequestBody @Valid ValidList<InventoryDTO> inventoryDTOList) {
        log.info("InventoryController batchInsert param:{}", JSON.toJSONString(inventoryDTOList));
        ChannelResult<Boolean> result = new ChannelResult<>();
        try {
            //数据转换
            List<InventoryParam> inventoryParamList = BeanCopyUtil.copyList(inventoryDTOList, InventoryParam.class);
            result = sunyurInventoryService.batchInsert(inventoryParamList);
        } catch (Exception e) {
            log.error("InventoryController batchInsert exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }

    /**
     * 批量查询库存台账
     */
    @PostMapping(value = "/listInventoryBookByParam", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult listInventoryBookByParam(@RequestBody @Valid ValidList<InventorySelectDTO> inventorySelectDTOList) {
        log.info("InventoryController listInventoryBookByParam param:{}", JSON.toJSONString(inventorySelectDTOList));
        ChannelResult result = new ChannelResult<>();
        try {
            //数据转换
            List<InventorySelectParam> inventorySelectParamList = BeanCopyUtil.copyList(inventorySelectDTOList, InventorySelectParam.class);
            result = sunyurInventoryService.listInventoryBookByParam(inventorySelectParamList);
        } catch (Exception e) {
            log.error("InventoryController listInventoryBookByParam exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }

    /**
     * 批量增加库存台账
     */
    @PostMapping(value = "/batchRelease", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult batchRelease(@RequestBody @Valid InventoryReleaseOrReduceDTO inventoryReleaseOrReduceDTO) {
        log.info("InventoryController batchRelease param:{}", JSON.toJSONString(inventoryReleaseOrReduceDTO));
        ChannelResult result = new ChannelResult<>();
        try {
            InventoryReleaseOrReduceParam inventoryReleaseOrReduceParam = getInventoryReleaseOrReduceParam(inventoryReleaseOrReduceDTO);
            result = sunyurInventoryService.batchRelease(inventoryReleaseOrReduceParam);
        } catch (Exception e) {
            log.error("InventoryController batchRelease exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }


    /**
     * 批量减少库存台账
     */
    @PostMapping(value = "/batchReduce", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult batchReduce(@RequestBody @Valid InventoryReleaseOrReduceDTO inventoryReleaseOrReduceDTO) {
        log.info("InventoryController batchReduce param:{}", JSON.toJSONString(inventoryReleaseOrReduceDTO));
        ChannelResult result = new ChannelResult<>();
        try {
            //创建减少库存台账参数
            InventoryReleaseOrReduceParam inventoryReleaseOrReduceParam = getInventoryReleaseOrReduceParam(inventoryReleaseOrReduceDTO);
            result = sunyurInventoryService.batchReduce(inventoryReleaseOrReduceParam);
        } catch (Exception e) {
            log.error("InventoryController batchReduce exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }


    /**
     * @return com.sunyur.tailor.channel.service.inner.inventory.param.InventoryReleaseOrReduceParam
     * @Author XuHui
     * @Description 库存台账加（减）数据转换
     * @Date 18:11 2021/12/29
     * @Param [inventoryReleaseOrReduceDTO]
     **/
    private InventoryReleaseOrReduceParam getInventoryReleaseOrReduceParam(InventoryReleaseOrReduceDTO inventoryReleaseOrReduceDTO) {
        //创建增加库存台账参数
        InventoryReleaseOrReduceParam inventoryReleaseOrReduceParam = new InventoryReleaseOrReduceParam();
        //数据转换
        List<RecordParamList> recordParamListDTO = inventoryReleaseOrReduceDTO.getRecordParamList();
        List<com.sunyur.tailor.channel.service.inner.inventory.param.RecordParamList> recordParamListList = BeanCopyUtil.copyList(recordParamListDTO, com.sunyur.tailor.channel.service.inner.inventory.param.RecordParamList.class);
        BeanUtils.copyProperties(inventoryReleaseOrReduceDTO, inventoryReleaseOrReduceParam);
        inventoryReleaseOrReduceParam.setRecordParamList(recordParamListList);
        return inventoryReleaseOrReduceParam;
    }
}